package com.crawler.utils.zilue;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 本类只是临时类，时间问题，我现在只放到了一个类里，后期会分类，并做成可拔插的小框架
 * 本类不建议大家修改，如果大家要修改请新建其他包和类（包名--自己的名字，类名--最好与本类一致）
 * Created by ZD on 2016/6/19.
 */
public class HttpUtils {
    /**
     * 修改前请看本类注释，最开始@全部人员
     * @throws IOException
     */
    @Test
    public void test1() throws IOException {
        String word="哪些汽车车祸率最高";
        String[] words={"车","车祸","率","高","哪些"};
        Integer page=0;
        CloseableHttpClient httpClient= HttpClientBuilder.create().build();
        HttpHost httpHost=new HttpHost("m.baidu.com",80);

        BasicHttpEntityEnclosingRequest httpRequest=new BasicHttpEntityEnclosingRequest("GET","/s?wd="+word+"&ie=utf-8");
//        BasicHttpEntityEnclosingRequest httpRequest=new BasicHttpEntityEnclosingRequest("GET","/search?q="+word+"&qs=n&pq="+word+"&sc=5-8&sp=-1&sk=&first="+(page*10+1)+"&FORM=PERE");

        String line = getString(httpClient, httpHost, httpRequest);
        Document document= Jsoup.parse(line);
        Elements tagAs = document.getElementsByTag("a");
        for (int i = 0; i < tagAs.size(); i++) {
            String content=tagAs.get(i).text();
            Integer time=0;
            for (int j = 0; j < words.length; j++) {
                if(content.contains(words[j])){
                    time++;
                    String bdUrl =tagAs.get(i).attr("href");

                    if(time>2&&bdUrl.startsWith("/from")){
                        System.out.println("bdURL:"+bdUrl);
                        httpRequest=new BasicHttpEntityEnclosingRequest("GET",bdUrl);
                        String bdLine=getString(httpClient,httpHost,httpRequest);
                        Document document2= Jsoup.parse(bdLine);
                        Elements metas = document2.getElementsByTag("meta");
                        String[] metaContents=null;
                        for (int k = 0; k < metas.size(); k++) {
                            String metaContent=metas.get(k).attr("content");
                            if(metaContent.contains("url=")){
                                metaContents=metaContent.split(";");
                                break;
                            }
                        }
                        if(metaContents==null){
                            continue;
                        }
                        String acturalUrl="";
                        for (int k = 0; k < metaContents.length; k++) {
                            if(metaContents[k].contains("url=")){
                                acturalUrl+=metaContents[k].replace("url=","").replace("'","").replace("\"","");
                                break;
                            }
                        }
                        System.out.println("URL地址："+acturalUrl);
                        System.out.println("URL内容："+tagAs.get(i).text());
                        break;
                    }
                }

            }
        }
//        System.out.println(line);
    }

    private String getString(CloseableHttpClient httpClient, HttpHost httpHost, BasicHttpEntityEnclosingRequest httpRequest) throws IOException {
        CloseableHttpResponse closeableHttpResponse = httpClient.execute(httpHost, httpRequest);
        HttpEntity httpEntity = closeableHttpResponse.getEntity();
        InputStream inputStream = httpEntity.getContent();
        StringBuffer stringBuffer=new StringBuffer();
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
        String line;
        while ( (line= bufferedReader.readLine())!=null){
            stringBuffer.append(line);
        }
        line=stringBuffer.toString();

        closeableHttpResponse.close();
        System.out.println(line);
        return line;
    }
}
